<template>
  <el-drawer
    ref="drawer"
    size="70%"
    title="分配"
    :visible.sync="visible"
    direction="rtl"
    custom-class="demo-drawer"
    :before-close="cancel"
  >
    <el-row :gutter="20">
      <!--部门数据-->
      <!-- <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-tree
            ref="tree"
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>-->
      <!--用户数据-->
      <el-col :span="24">
        <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
          <el-form-item label="用户名称" prop="nickName">
            <el-input
              v-model="queryParams.nickName"
              placeholder="请输入用户名称"
              clearable
              size="small"
              style="width: 140px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="手机号码" prop="phone">
            <el-input
              v-model="queryParams.phone"
              placeholder="请输入手机号码"
              clearable
              size="small"
              style="width: 140px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <!-- <el-form-item label="岗位" prop="postId">
            <el-select
              v-model="queryParams.postId"
              style="width: 160px"
              filterable
              placeholder="请选择"
            >
              <el-option
                v-for="item in postOptions"
                :key="item.postId"
                :label="item.postName"
                :value="item.postId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="用户状态"
              clearable
              size="small"
              style="width: 160px"
            >
              <el-option
                v-for="dict in statusOptions"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item> -->
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="userList" highlight-current-row border>
          <el-table-column width="35px">
            <template v-slot="scope">
              <el-radio v-model="userId" :label="scope.row.userId" @change="handleRowChange(scope.row)">{{ "" }}
              </el-radio>
            </template>
          </el-table-column>
          <el-table-column label="序号" width="75" type="index" />
          <el-table-column label="用户名称" prop="nickName" :show-overflow-tooltip="true" />
          <el-table-column label="部门" prop="dept.deptName" :show-overflow-tooltip="true" />
          <el-table-column label="状态" prop="status">
            <template slot-scope="scope">
              <span>{{ scope.row.status === '2' ? '启用' : '禁用' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="手机号码" prop="phone" />
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageIndex"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <div style="position: fixed;bottom: 10px;right: 30px">
      <el-button type="primary" @click="submitUser">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-drawer>
</template>
<script>
import { listUser } from '@/api/admin/sys-user'
import { treeselect } from '@/api/admin/sys-dept'
import { listPost } from '@/api/admin/sys-post'
export default {
  name: 'UserList',
  props: {
    visible: {
      type: Boolean,
      default: () => {
        return false
      }
    }
  },
  data() {
    return {
      // 查询参数
      queryParams: {
        pageIndex: 1,
        pageSize: 8,
        nickName: undefined,
        phone: undefined,
        status: '2',
        deptId: undefined,
        postId: '[49,50,51,52]'
      },
      loading: false,
      total: 0,
      userList: [],
      // 部门树选项
      deptOptions: undefined,
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 状态数据字典
      statusOptions: [
        { label: '启用', value: '2' },
        { label: '禁用', value: '1' }
      ],
      userId: null,
      // 岗位选项
      postOptions: []
    }
  },
  watch: {
    visible: {
      handler(newVal) {
        if (newVal === true) {
          this.getList()
        } else {
          this.resetForm('queryForm')
        }
      }
    }
  },
  created() {
    treeselect().then(response => {
      this.deptOptions = response.data
    })
    listPost({ pageSize: 1000 }).then(response => {
      this.postOptions = response.data.list
    })
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true
      listUser(this.queryParams).then(response => {
        this.userList = response.data.list.filter(item => item.userId !== 1)
        this.total = response.data.count
        this.loading = false
      })
    },

    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },

    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id
      this.getList()
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageIndex = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleRowChange(row) {
      this.userId = row.userId
    },
    submitUser() {
      this.$emit('submitUser', this.userId)
    },
    cancel() {
      this.$emit('cancelUser')
    }
  }
}
</script>
